function [R E]= rs_errors(T,err,ErrorType);
%
% [R E]= rs_errors(T,err,ErrorType)
% 
% ErrorType determines how the errors will be calculated. If it is 'bler',
% then the err variable is treated as the block error rate that will be
% used to generate errors. If it is ‘num’, then the err variable represents
% a fixed number of errors (but at random locations) that will be
% introduced to codeword T.
% 
%
% Tansu Demirbilek
% ademirbi@mc.com
% Mercury Computer Systems

N_Tries = size(T,1);
n = size(T,2);
N = 2^T.m-1;

if(~isstr(ErrorType))
    error('ErrorType must be a string');
end

% First choose the symbols that have error in them

if(lower(ErrorType) == 'bler')
    % BLER Based Errors
    BLER = err;
    E = gf(rand(1,n*N_Tries)<BLER, T.m,T.prim_poly); % Only n symbols per codeword are transmitted
    ind = find(E==1);
    % error values are between 1..N
    err = gf(floor( N * rand(1,length(ind) ) + 1),T.m,T.prim_poly);
    E(ind) = err;
    E = reshape(E,N_Tries,n);
elseif(lower(ErrorType)=='num')
    % Fixed number of errors
    numErrors = err;
    E = gf(randcomb(n, numErrors, N_Tries), T.m,T.prim_poly);
    ind = find(E==1);
    % Randomly generate error values between 1 and n
    err = gf(floor((N)*rand(1,length(ind)) + 1),T.m,T.prim_poly);
    E(ind) = err;
else
    error('ErrorType should either be BLER or NUM');
end

% Received vector
R = E+T;

